package com.wonders.frame.controller;

import com.gcr.server.response.Response;
import com.gcr.server.response.ResponseCodeEnum;
import com.wonders.frame.common.Enum.InitCommonSourceEnum;
import com.wonders.frame.common.ResponseUtil;
import com.wonders.frame.controller.core.CoreController;
import com.wonders.frame.entity.AuthPermission;
import com.wonders.frame.entity.Tree.TreeNodeData;
import com.wonders.frame.entity.form.AuthPermissionForm;
import com.wonders.frame.service.AuthPermissionService;
import com.wonders.frame.service.Core.CoreCurdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;


@RestController
@RequestMapping("/authPermission")
public class AuthPermissionController extends CoreController<AuthPermission> {
    Logger log = LoggerFactory.getLogger(AuthPermissionController.class);
    @Autowired
    private AuthPermissionService service;

    @Override
    protected CoreCurdService getService() {
        return service;
    }

    private final ReentrantLock lock=new ReentrantLock();

    @RequestMapping("/save")
    public Response save(@RequestBody AuthPermissionForm permissionForm) {
        try {
            lock.lock();
            log.info("request:authPermission/save");
            log.info(permissionForm.toString());
            if (service.checkUnique(permissionForm.getId(),permissionForm.getCode())) {
                permissionForm.setComponentKey(permissionForm.getCode());
                AuthPermission authPermission = service.save(permissionForm);
                InitCommonSourceEnum.INSTANCE.initPermission();
                return successResponse(authPermission);
            } else {
                return errorResponse(-2, "权限代码或名字重复", null);
            }
        } catch (Exception e) {
            return errorResponse();
        }finally {
            lock.unlock();
        }
    }

    @RequestMapping("deleteByPermissionId")
    public Response deleteByPermissionId(String permissionId) {
        try {
            //删除该权限  并刷新内存的权限树
            service.deleteByPermissionId(permissionId);
            return successResponse(null);
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }
    }


    @RequestMapping("getMenuNodeTree")
    public List<TreeNodeData> getMenuNodeTree() {
        return service.getMenuNodeTree();
    }


    @GetMapping("/getPermissionTree")
    public Response getPermissionTree() {
        try {
            log.info("request:authPermission/getPermissionTree");
            return ResponseUtil.success(service.getPermissionTree());
        } catch (Exception e) {
            return ResponseUtil.error();
        }

    }

    @RequestMapping("getMenuHtml")
    public Response getMenuHtml() {
        Response res = new Response();
        res.setCode(ResponseCodeEnum.SUCCESS.getCode());
        res.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        res.setContent(service.getMenuHtml());
        return res;
    }

    @RequestMapping("getAll")
    public Response getAll() {
        Response res = new Response();
        res.setCode(ResponseCodeEnum.SUCCESS.getCode());
        res.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        res.setContent(service.getAll());
        return res;
    }

    @RequestMapping("getRolePermission")
    public Response getRolePermission() {
        Response res = new Response();
        res.setCode(ResponseCodeEnum.SUCCESS.getCode());
        res.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        res.setContent(service.getAll());
        return res;
    }

    //模糊查询节点 findNodeByPermissionName
    @GetMapping("/findNodeByPermissionName")
    public Response findNodeByPermissionName(String permissionName) {
        try {
            return successResponse(service.searchNodeByPermissionName(permissionName));
        } catch (Exception e) {
            e.printStackTrace();
            return errorResponse();
        }
    }
}
